﻿using System;
using System.Collections.Generic;


namespace QSharpTest.Utility
{
    class RandomSelector
    {
        public RandomSelector(Random r)
        {
            mRandom = r;
        }

        public RandomSelector(int seed)
        {
            mRandom = new Random(seed);
        }

        public RandomSelector()
        {
            mRandom = new Random();
        }

        public int[] Get(int n)
        {
            int[] a = new int[n];

            LinkedList<int> list = new LinkedList<int>();
            LinkedListNode<int> node = list.AddFirst(0);
            for (int i = 1; i < n; i++)
                node = list.AddAfter(node, i);

            for (int i = 0; i < n; i++)
            {
                int r = mRandom.Next(list.Count);
                node = list.First;
                for (; r > 0; r--)
                {
                    node = node.Next;
                }
                a[i] = node.Value;
                list.Remove(node);
            }

            return a;
        }

        protected Random mRandom;
    }
}
